package com.cnstrong.webview2.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.BuildConfig;

public class MagicGestureView extends FrameLayout {

    private static final String TAG = "MagicGestureView";

    private MagicGesturePolicy mMagicGesturePolicy;
    private MagicGesture mClickRefreshGesture;

    private float mDownX;
    private float mDownY;
    private Paint mPaint;

    public MagicGestureView(@NonNull Context context) {
        super(context);
        setClickable(true);
        initPaint();
    }

    public MagicGestureView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setClickable(true);
        initPaint();
    }

    public MagicGestureView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setClickable(true);
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED); //设置圆环的颜色
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mMagicGesturePolicy == null) {
            mMagicGesturePolicy = new MagicGesturePolicy(this);
        }
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownX = ev.getX();
                mDownY = ev.getY();
                break;
            case MotionEvent.ACTION_UP:
                final MagicGesture magicGesture = new MagicGesture(this, mDownX, mDownY, ev.getX(), ev.getY());
                boolean result = mMagicGesturePolicy.addGesture(magicGesture);
                if (result || mMagicGesturePolicy.addLeakGesture(magicGesture)) {
                    // 如果这个手机收集成功，时间将不会传递下去，将会被消费掉
                    invalidate();
                    return true;
                } else {
                    mClickRefreshGesture = null;
                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        // Debug模式下，显示点击区域
        if (BuildConfig.DEBUG) {
            if (mMagicGesturePolicy != null) {
                mMagicGesturePolicy.drawMagicGesture(canvas, mPaint);
            }
            if (mClickRefreshGesture != null) {
                canvas.drawRect(mClickRefreshGesture.getClickRefreshRect(), mPaint);
            }
        }
    }


    private GestureProcessor mGestureProcessor;

    public void setGestureProcessor(GestureProcessor processor) {
        if (processor != null) {
            mGestureProcessor = processor;
        }
    }

    public GestureProcessor getGestureProcessor() {
        return mGestureProcessor;
    }


    public interface GestureProcessor {
        void handleGesture(MagicGestureEvent event);
    }
}
